﻿using UnityEngine;
using System.Collections;

public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
    protected static T _Instance;
    protected static object _lock = new object();
    public static T Instance
    {
        get
        {
            lock (_lock)
            {
                if (_Instance == null)
                {
                    _Instance = GameObject.FindObjectOfType<T>();
                    if (_Instance == null)
                    {
                        GameObject instance = new GameObject(typeof(T).ToString());
                        GameObject.DontDestroyOnLoad(instance);
                        _Instance = instance.AddComponent<T>();
                    }
                }
                return _Instance;
            }
        }
    }
}
